package updateTask;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import page.PageState;
import task.Task;
import task.taskCheck;
import usr.Usr;

public class UpdateTask extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	@SuppressWarnings("deprecation")
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	{	//update task's decision by taskid, drive by "editTask.jsp"
		try{
			HttpSession sess = req.getSession();
			Usr usr = (Usr)sess.getAttribute("usr");
			if(usr == null)
			{
				req.setAttribute("error_cause", PageState.LOGIN_NOT_EXIST);
				req.getRequestDispatcher("login.jsp").forward(req, resp);
				return;
			}
			
			int taskid = Integer.parseInt(req.getParameter("taskid"));
			String desc = (String)req.getParameter("desc");
			if(taskCheck.CheckUsr(taskid, usr.getId()) == false)
			{
				req.setAttribute("error_cause", PageState.LOGIN_NOT_EXIST);
				req.getRequestDispatcher("login.jsp").forward(req, resp);
				return;
			}
			
			Task task = new Task();
			task.setId(taskid);
			task.update();
			task.setDesc(desc);
			task.modify();
			
			resp.sendRedirect("tasks.jsp");
		}
		catch(IOException | ServletException | SQLException  e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	
}
